<script setup>
import { defineEmits, ref,onMounted } from 'vue'
import {pageApi, saveApi, updateApi} from "../../../api/menu.js";
import {ElMessage} from "element-plus";

//定义事件--父级
const emit = defineEmits(['flush']);

const tableData=ref({})
onMounted(async ()=>{
  const r=await pageApi({page:1,size:10});
  tableData.value=r.data;
})
// 表单数据
const formData = ref({
  id: 0,
  component: "",
  icon: "",
  level: 0,
  name: "",
  parentId: 0,
  path: "",
  sort: 0,
  type: 0
})
// 表单验证
const formRules = ref({
  name: [
    { required: true, message: '请输入名称', trigger: 'blur' },
    { min: 2, max: 20, message: '请输入2~20个字符', trigger: 'blur' }
  ]
})
// 提交验证
const formRef = ref()
const onSubmit = () => {
  console.log("id  "+formData.value.id)
  formRef.value.validate(async (isSuccess) => {
    if (isSuccess) {

      let r;
      const isadd=formData.value.id==0||formData.value.id==undefined;
      if(isadd) {
        r=await saveApi(formData.value);
      }else {
        r=await updateApi(formData.value);
      }
      if(r.code==0){
        ElMessage({
          type: 'success',
          message: isadd?'新增菜单成功':'修改菜单成功'})
        state.value=false
        emit('flush')
      }else {
        ElMessage.error(r.data)
      }
    }
  })
}
// 对话框状态
const state = ref(false)

const props = {
  children: 'children',
  label: 'name',
  value: 'id'
}

// 暴露
defineExpose({
  state,
  formData
})
</script>
<template>
  <el-dialog
    v-model="state"
    :title="`${formData.id>0? '修改' : '添加'}菜单`"
    width="600"
    :before-close="() => (state = false)"
  >
    <!-- 内容 -->
    <el-form
      v-if="state"
      size="large"
      label-width="80"
      :model="formData"
      :rules="formRules"
      ref="formRef"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="菜单名称" prop="name">
            <el-input placeholder="请输入菜单名称" v-model="formData.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="sort">
            <el-input type="number" v-model="formData.sort"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
      <el-form-item label="菜单类型" prop="">
        <el-radio-group v-model="formData.type" class="ml-4">
          <el-radio :label="1" size="large">菜单</el-radio>
          <el-radio :label="2" size="large">权限</el-radio>
        </el-radio-group>
      </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单图标">
            <el-input placeholder="请输入菜单图标" v-model="formData.icon" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="菜单等级" prop="">
        <el-radio-group v-model="formData.level" class="ml-4">
          <el-radio :label="1" size="large">一级</el-radio>
          <el-radio :label="2" size="large">二级</el-radio>
          <el-radio :label="3" size="large">三级</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="所属菜单">
        <el-tree-select
            v-model="formData.parentId"
            :data="[{ name: '一级菜单', id: 0, children: tableData.data }]"
            :props="props"
            check-strictly=true
            :render-after-expand="true"
        />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="路径|权限">
            <el-input type="text" v-model="formData.path"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="路由名称">
            <el-input type="text" v-model="formData.component"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="default" @click="state = false">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 内容 -->
  </el-dialog>
</template>
